package com.lemon.kenhnhacvn;

import java.lang.ref.WeakReference;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Enumeration;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.text.format.Formatter;
import android.util.Base64;
import android.util.Log;
import android.view.Window;

public class SplashActivity extends FragmentActivity {
	String TAG = SplashActivity.class.getSimpleName();

	private MyHandler mHandler = new MyHandler(this);
	private final int mDelayMillis = 1000;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.welcome);
		
		mHandler.sendEmptyMessageDelayed(0, mDelayMillis);
		getKeyHashes(this);
	}
	@TargetApi(Build.VERSION_CODES.FROYO)
	public static final void getKeyHashes(Context context) {
		PackageInfo packageInfo;
		try {
			// packageInfo =
			// getPackageManager().getPackageInfo("casino.vn",PackageManager.GET_SIGNATURES);
			packageInfo = context.getPackageManager().getPackageInfo(context.getPackageManager().getPackageInfo(context.getPackageName(), 0).packageName,
					PackageManager.GET_SIGNATURES);
			for (Signature signature : packageInfo.signatures) {
				MessageDigest md = MessageDigest.getInstance("SHA");
				md.update(signature.toByteArray());
				String key = new String(Base64.encode(md.digest(), 0));
				// String key = new String(Base64.encodeBytes(md.digest()));
				Log.e("Hash key", key);
			}
		} catch (NameNotFoundException e1) {
			Log.e("Name not found", e1.toString());
		} catch (NoSuchAlgorithmException e) {
			Log.e("No such an algorithm", e.toString());
		} catch (Exception e) {
			Log.e("Exception", e.toString());
		}
	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	public void onBackPressed() {
	}

	private static void switchMain(SplashActivity mActivity) {
		mActivity.startActivity(new Intent(mActivity, MainActivity.class));
		mActivity.finish();
	}

	private static class MyHandler extends Handler {
		WeakReference<SplashActivity> mFragmentWeakReference = null;
		SplashActivity mActivity = null;

		public MyHandler(SplashActivity a) {
			mFragmentWeakReference = new WeakReference<SplashActivity>(a);
			mActivity = mFragmentWeakReference.get();
		}

		@Override
		public void handleMessage(Message msg) {
			switchMain(mActivity);
		}
	}
}
